﻿using Syncfusion.UI.Xaml.Grid;
using System.Windows;
using Syncfusion.Data;
namespace Syncfusion.Lib.Controls
{
    /// <summary>
    /// 通用 <see cref="SfDataGrid"/>
    /// </summary>
    [StyleTypedProperty(Property = "CellStyle", StyleTargetType = typeof(GridCell))]
    [StyleTypedProperty(Property = "UnBoundRowCellStyle", StyleTargetType = typeof(GridUnBoundRowCell))]
    [StyleTypedProperty(Property = "RowStyle", StyleTargetType = typeof(VirtualizingCellsControl))]
    [StyleTypedProperty(Property = "UnBoundRowStyle", StyleTargetType = typeof(UnBoundRowControl))]
    [StyleTypedProperty(Property = "HeaderStyle", StyleTargetType = typeof(GridHeaderCellControl))]
    [StyleTypedProperty(Property = "CaptionSummaryRowStyle", StyleTargetType = typeof(CaptionSummaryRowControl))]
    [StyleTypedProperty(Property = "TableSummaryRowStyle", StyleTargetType = typeof(TableSummaryRowControl))]
    [StyleTypedProperty(Property = "GroupSummaryRowStyle", StyleTargetType = typeof(GroupSummaryRowControl))]
    [StyleTypedProperty(Property = "GroupSummaryCellStyle", StyleTargetType = typeof(GridGroupSummaryCell))]
    [StyleTypedProperty(Property = "CaptionSummaryCellStyle", StyleTargetType = typeof(GridCaptionSummaryCell))]
    [StyleTypedProperty(Property = "TableSummaryCellStyle", StyleTargetType = typeof(GridTableSummaryCell))]
    [StyleTypedProperty(Property = "GroupDropAreaStyle", StyleTargetType = typeof(GroupDropArea))]
    [TemplatePart(Name = "PART_GroupDropArea", Type = typeof(GroupDropArea))]
    [TemplatePart(Name = "PART_VisualContainer", Type = typeof(VisualContainer))]
    [StyleTypedProperty(Property = "DetailsViewDataGridStyle", StyleTargetType = typeof(DetailsViewDataGrid))]
    [StyleTypedProperty(Property = "FilterPopupStyle", StyleTargetType = typeof(GridFilterControl))]
    public class SfDataGridGeneral : SfDataGrid
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SfDataGridGeneral()
        {
            this.AllowDraggingColumns = true;
            this.AllowFiltering = true;
            this.AllowResizingColumns = true;
            this.AllowResizingHiddenColumns = true;
            this.AutoGenerateColumns = false;
            this.ClipToBounds = true;
            this.ColumnSizer = GridLengthUnitType.AutoLastColumnFill;
            //this.FooterColumnCount = 1;
            this.GridCopyOption = GridCopyOption.CopyData | GridCopyOption.IncludeHeaders;
            this.RowHeaderWidth = 50;
            this.SelectionMode = GridSelectionMode.Single;
            this.SelectionUnit = GridSelectionUnit.Row;
            this.ShowRowHeader = true;
            this.ShowSortNumbers = true;
            this.AllowTriStateSorting = true;
            this.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
            this.SetResourceReference(StyleProperty, "SyncfusionSfDataGridStyle");
            this.QueryColumnDragging += SfDataGridGeneral_QueryColumnDragging;
            //this.SearchHelper = new SfDataGirdSearchHelper(this);
        }

        private void SfDataGridGeneral_QueryColumnDragging(object? sender, QueryColumnDraggingEventArgs e)
        {
            if (e.Reason == QueryColumnDraggingReason.Dropping)
            {
                //限制冻结列和非冻结列之间的排序拖动交互
                //1.判断拖动的列是否在冻结列中
                bool fromIsFrozenColumn = e.From < this.FrozenColumnCount || e.From >= (this.Columns.Count - this.FooterColumnCount);
                //2.判断拖动的模板列是否在冻结列中
                bool toIsFrozenColumn = e.To < this.FrozenColumnCount || e.To >= (this.Columns.Count - this.FooterColumnCount);
                if(fromIsFrozenColumn != toIsFrozenColumn)
                {
                    e.Cancel = true;
                }

            }
        }
    }
}
